/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */

function ListNode(val, next) {
  this.val = (val === undefined ? 0 : val)
  this.next = (next === undefined ? null : next)
}

/**
 * @param {ListNode} head
 * @return {void} Do not return anything, modify head in-place instead.
 */
var reorderList = function (head) {


  // 所有节点入栈
  let stack = []
  let p = head
  while (p) {
    stack.push(p)
    p = p.next
  }
  if (stack.length > 2) {
    p = head
    while (p.next) {
      let insertNode = stack.pop()
      let tmp = p.next
      p.next = insertNode
      insertNode.next = tmp
      p = tmp
      if (p.next == insertNode) {
        p.next = null
      }
    }
  }
  return head
};

let l = {
  val: 1,
  next: {
    val: 2,
    next: {
      val: 3,
      next: {
        val: 4,
        next: {
          val: 5,
          next: {
            val: 6,
            next: {
              val: 7,
              next: null
            }
          }
        }
      }

    }
  }
}


console.log(reorderList(l));